package demo1;

import java.util.Stack;

//括号匹配
public class Test3 {
    public static void main(String[] args) {
        Stack<Character> stack = new Stack<>();
        String str = "()";
        char ch = ' ';
        for (int i = 0; i < str.length(); i++) {
            ch = str.charAt(i);
            if (isLeft(ch)) {
                stack.push(ch);
            } else {
                Character pop = ' ';
                if (!stack.empty()) {
                    pop = stack.pop();
                } else {
                    //return false;
                    break;
                }
                if (!isMatching(pop,ch)) {
                    //return false;
                    break;
                }
            }
        }
        //return stack.empty();
        System.out.println(stack.empty());
    }
    //判断是不是左括号
    public static boolean isLeft(char ch) {
        if (ch == '(' || ch == '{' || ch == '[') {
            return true;
        }
        return false;
    }



    //判断左右括号是否匹配
    public static boolean isMatching(char ch1, char ch2) {
        if ((ch1 == '(' && ch2 == ')')
            || (ch1 == '[' && ch2 == ']')
            || (ch1 == '{' && ch2 == '}')) {
            return true;
        }
        return false;
    }
}
